package top.lilele.adminSystem.config;

import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.interceptor.SaRouteInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.lilele.adminSystem.entity.AdminResourceEntity;
import top.lilele.adminSystem.mapper.AdminResourceMapper;
import top.lilele.adminSystem.utils.Utils;

import javax.annotation.Resource;
import java.util.List;

/**
 * sa-token路由拦截器配置
 *
 * @author lilele
 */
@Configuration
@Slf4j
public class SaTokenConfigure implements WebMvcConfigurer {
    @Resource
    AdminResourceMapper adminResourceMapper;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SaRouteInterceptor((req, res, handler) -> {
            // 设置白名单登录校验、直接进入controller。
            SaRouter.match("/**").notMatch(Utils.authWhiteList).check(r -> {
                StpUtil.checkLogin();
            });
            // 查询所有的api权限资源
//            QueryWrapper queryWrapper = new QueryWrapper();
//            queryWrapper.eq("menuType", 1);
//            List<AdminResourceEntity> list = adminResourceMapper.selectList(queryWrapper);
//            // 设置所有的权限校验
//            list.stream().forEach(item -> {
//                SaRouter.match(item.getUrl(), r -> StpUtil.checkPermission(item.getName()));
//            });
        })).addPathPatterns("/**");
    }

    @Bean
    @Primary
    public SaTokenConfig getSaTokenConfigPrimary() {
        SaTokenConfig config = new SaTokenConfig();
        config.setTokenName("Authorization");
        config.setIsConcurrent(false);
        config.setIsReadBody(false);
        config.setIsReadCookie(false);
        config.setIsPrint(false);
        config.setTimeout(30 * 24 * 60 * 60);
        config.setActivityTimeout(-1);
        config.setTokenStyle("random-64");
        config.setTokenSessionCheckLogin(false);
        return config;
    }

    @Bean
    public CorsFilter corsWebFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        config.setMaxAge(10000L);
        config.addExposedHeader("Content-Disposition");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}
